﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WinAPITest
{
    class Program
    {
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_LBUTTONUP = 0x0202;
        private const int WK_DOWN = 0x28;
        private const int WK_RETURN = 0x0D;
        private const int LB_GETCOUNT = 0x018B; 
        private const int LB_GETTEXT = 0x0189;

        static void Main(string[] args)
        {
            int hwnd = 0;
            IntPtr hwndChild = IntPtr.Zero;

            hwnd = APIFuncs.FindWindow(null, "Total Commander 7.56a - RWE IT GmbH");
            Console.WriteLine(hwnd);
            var length = APIFuncs.GetWindowTextLength(((IntPtr)hwnd));
            var sb = new StringBuilder(length+1);
            APIFuncs.GetWindowText((IntPtr) hwnd, sb, sb.Capacity);
            Console.WriteLine(sb.ToString());
            if (hwnd != 0)
            {
                foreach (var s in APIFuncs.GetChildWindows((IntPtr)hwnd))
                {
                    if ((int)s == 199084)
                    {
                        var list = GetListBoxContents(s);
                        foreach (var item in list)
                        {
                            Console.WriteLine(item);
                        }
                        APIFuncs.SetForegroundWindow(s);
                        SendKeys.SendWait("{DOWN}");
                        SendKeys.SendWait("{DOWN}");
                        SendKeys.SendWait("{ENTER}");
                        //APIFuncs.SetFocus(new HandleRef(null, s));
                        //APIFuncs.SendMessage((int)s, WK_DOWN, 50, IntPtr.Zero);
                        //APIFuncs.SendMessage((int)s, WK_DOWN, 0, IntPtr.Zero);
                        //APIFuncs.SendMessage((int)s, WK_RETURN, 0, IntPtr.Zero);
                    }

                    //APIFuncs.SendMessage((int) s, WK_DOWN, 0, IntPtr.Zero);
                    //APIFuncs.SendMessage((int) s, WK_DOWN, 0, IntPtr.Zero);
                    //APIFuncs.SendMessage((int) s, WK_RETURN, 0, IntPtr.Zero);
                    //if ((int)s == 197972)
                    //{
                    //    APIFuncs.SetForegroundWindow(s);
                    //    Rectangle re;
                    //    APIFuncs.GetWindowRect(new HandleRef(null, s), out re); 
                    //    Cursor.Position = new Point((re.X + re.Width) / 2, (re.Y + re.Height) / 2);
                    //    APIFuncs.SendMessage((int)s, WM_LBUTTONDOWN, 0, IntPtr.Zero);
                    //    APIFuncs.SendMessage((int)s, WM_LBUTTONUP, 0, IntPtr.Zero);

                    //    //APIFuncs.SetFocus(new HandleRef(null, s));
                    //    var l = APIFuncs.GetWindowTextLength(s);
                    //    var stringBuffer = new StringBuilder(l + 1);
                    //    APIFuncs.GetWindowText(s, sb, sb.Capacity);
                    //    Console.WriteLine(stringBuffer.ToString());
                       
                    //    //SendKeys.SendWait("{DOWN}");
                    //    //APIFuncs.SendMessage((int) s, WK_DOWN, 0, IntPtr.Zero);
                    //    //APIFuncs.SendMessage((int) s, WK_DOWN, 0, IntPtr.Zero);
                    //    APIFuncs.SendMessage((int) s, WK_RETURN, 0, IntPtr.Zero);

                    //    //APIFuncs.SendMessage((int)s, WM_LBUTTONDOWN, 0, IntPtr.Zero);
                    //    //APIFuncs.SendMessage((int)s, WM_LBUTTONUP, 0, IntPtr.Zero);
                    //    //var l = APIFuncs.GetWindowTextLength(((IntPtr)hwnd));
                    //    //var stringBuffer = new StringBuilder(l + 1);
                    //    //APIFuncs.GetWindowText(s, sb, sb.Capacity);
                    //    //Console.WriteLine(stringBuffer.ToString());

                    //}
                }
            }
            Console.ReadLine();
        }

        private static List<string> GetListBoxContents(IntPtr listBoxHwnd)
        {
            int cnt = (int)APIFuncs.SendMessage(listBoxHwnd, LB_GETCOUNT, IntPtr.Zero, null);
            List<string> listBoxContent = new List<string>();
            for (int i = 0; i < cnt; i++)
            {
                StringBuilder sb = new StringBuilder(256);
                var getText = APIFuncs.SendMessage(listBoxHwnd, LB_GETTEXT, (IntPtr)i, sb);
                listBoxContent.Add(sb.ToString());
            }
            return listBoxContent;
        }

        //private static void Test()
        //{
        //    int hwnd = 0;
        //    IntPtr hwndChild = IntPtr.Zero;

        //    hwnd = FindWindow(null, "Microsoft Outlook");
        //    if (hwnd != 0)
        //    {
        //        SetForegroundWindow((IntPtr)hwnd);
        //        hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Window", "MsoDockLeft");
        //        if (hwndChild != IntPtr.Zero)
        //        {
        //            SendMessage((int) hwndChild, WM_LBUTTONDOWN, 0, IntPtr.Zero);
        //            SendMessage((int) hwndChild, WM_LBUTTONUP, 0, IntPtr.Zero);
        //        }
        //        else
        //        {
        //            //...
        //        }
        //    }
        //    else
        //    {
        //        //...
        //    }
        //    hwnd = 0;
        //}

    }
}
